//
//  JKTextViewController.swift
//  JKSwiftTextView
//
//  Created by 王冲 on 2018/1/22.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

class JKTextViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        /**
            1.控制器的基本设置
         */
        title = "JKTextView"
        view.backgroundColor = UIColor.white
        self.edgesForExtendedLayout = []
        
        /**
            2.添加UITextView和提示内容以及键盘上的view
         */
        view.addSubview(textView)
        textView.addSubview(placeholderLabel)
        textView.inputAccessoryView = jkInputAccessoryView
    }
    
    // MARK: UITextView 的创建
    private lazy var textView:UITextView = {
        
        let textview = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64))
        textview.font = UIFont.systemFont(ofSize: 20)
        // 垂直水平方向有弹性
        textview.alwaysBounceVertical = true
        // 垂直方向滑动键盘下去
        textview.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag
        return textview
    }()
  
    // MARK: UILabel 提示文本
    private lazy var placeholderLabel: UILabel = {
        
        let label = UILabel(frame: CGRect(x: 5, y: 9, width: UIScreen.main.bounds.width-10, height: 22))
        label.font = UIFont .systemFont(ofSize: 20)
        label.textColor = UIColor.red
        label.text = "分享新鲜事"
        return label
        
    }()
    
    // MARK: UIView 键盘上加个view
    private lazy var jkInputAccessoryView: UIView = {
        
        let inputView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
        inputView.backgroundColor = UIColor.green
        return inputView
        
    }()
}

// MARK: UITextView代理的实现
extension JKTextViewController: UITextViewDelegate{
    
    func textViewDidChange(_ textView: UITextView) {
        
        /**
            hasText 可以判断UITextView是否有内容
         */
        placeholderLabel.isHidden = textView.hasText

    }
    
}
